package com.qd.core.service;

import com.cdqidi.util.DateUtil;
import com.cdqidi.util.SpringContextHolder;
import com.qd.hik.dto.parkinfo.HkParkReq;
import com.qd.panda.gas.sync.EGasSyncService;
import com.qd.panda.gas.sync.LastShellSyncService;
import com.qd.panda.service.commodity.CommodityStatisticsService;
import com.qd.panda.service.third.hk.HkParkSyncService;
import com.qd.panda.service.wallet.publish.PlanPublishService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author sjk
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class TimeTaskService {
    //    private final UserCardService userCardService;
    private final LastShellSyncService lastShellSyncService;
    //    private final ShellHighSyncService shellHighSyncService;
    private final EGasSyncService eGasSyncService;
    //    private final CardPlanHolderService cardPlanHolderService;
//    private final ParkSpaceHolderService parkSpaceHolderService;
    private final HkParkSyncService hkParkSyncService;
    private final PlanPublishService planPublishService;
    private final CommodityStatisticsService commodityStatisticsService;

//    /**
//     * 每天晚上12点更新已结束的车位租赁
//     */
//    @Scheduled(cron = "0 05 0 ? * *")
//    public void updateEnd() {
//        log.info("更新已结束的车位租赁信息: " + LocalDateTime.now());
//        parkSpaceHolderService.updateEnd();
//    }

//    /**
//     * 每天中午12点发送短租车位到期提醒/优惠卷到期提醒(小程序消息)
//     */
//    @Scheduled(cron = "0 40 11 * * ?")
//    public void send() {
//        LocalDate localDate = LocalDate.now();
//        LocalDateTime now = LocalDateTime.now();
//        log.info("短租车位到期提醒: " + now);
//        parkSpaceHolderService.sendExpireMsg(localDate);
//        log.info("优惠卷到期提醒: " + now);
//        userCardService.reminder(localDate);
//    }

    /**
     * 每天晚上0点11分同步加油站数据
     */
    @Scheduled(cron = "0 11 0 ? * *")
    public void shellSync() {
        log.info("同步壳牌普通加油站数据: {}", LocalDateTime.now());
        lastShellSyncService.sync();
    }

//    /**
//     * 每天晚上0点21同步加油站数据
//     */
//    @Scheduled(cron = "0 21 0 ? * *")
//    public void shellHighSync() {
//        log.info("同步壳牌高速加油站数据: " + LocalDateTime.now());
//        shellHighSyncService.sync();
//    }


//    /**
//     * 每天凌晨1点清理过期的卷信息
//     */
//    @Scheduled(cron = "0 0 1 * * ?")
//    public void cleanUpCard() {
//        log.info("清理过期的卷信息: " + LocalDateTime.now());
//        cardPlanHolderService.cleanUpCard();
//    }

    /**
     * 每个小时同步一次易加油数据
     */
    @Scheduled(fixedDelay = 60 * 60 * 1000, initialDelay = 60 * 60 * 1000)
    public void eGasSync() {
        String activeProfile = SpringContextHolder.getActiveProfile();
        if (activeProfile.equals(SpringContextHolder.PROFILE_OFFICIAL)) {
            long start = System.currentTimeMillis();
            eGasSyncService.sync();
            long end = System.currentTimeMillis();
            log.info("同步易加油数据,耗时:{}s", (end - start) / 1000);
        } else {
            log.info("非正式版本不同步易加油数据");
        }
    }

    /**
     * 每半小时同步一次海康停车场数据
     */
    @Scheduled(fixedDelay = 30 * 60 * 1000, initialDelay = 30 * 60 * 1000)
    public void hkParks() {
        LocalDateTime now = LocalDateTime.now();
        log.info("同步海康停车场数据: {}", now);
        HkParkReq req = new HkParkReq();
        req.setPageNo(1);
        req.setPageSize(99);
        hkParkSyncService.parks(req, now);
        req.freeData();
    }

    /**
     * 每20分钟同步商品统计数据到数据中台
     */
    @Scheduled(fixedDelay = 20 * 60 * 1000, initialDelay = 5 * 60 * 1000)
    public void commodityStatistics() {
        LocalDateTime now = LocalDateTime.now();
        commodityStatisticsService.commodityStatistics();
        log.info("同步商品统计数据: {}", now);

    }

    /**
     * 清理过期发布套餐
     */
    @Scheduled(cron = "0 0 0 * * ?")
    @Scheduled(fixedDelay = 30 * 60 * 1000, initialDelay = 30 * 60 * 1000)
    public void cleanExpiredPlan() {
        log.info("清理过期发布套餐: {}", DateUtil.getStringNowDateTime());
        planPublishService.cleanExpiredPlan();
    }
}